package org.cmcc.pcap.packet.udp;

import com.cmcc.xdr.db.PcapUdpDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.pcap4j.packet.UdpPacket;

import java.util.ArrayList;
import java.util.Date;

/**
 * @Auther: zhida wei
 * @Date: 2019/6/17 18:06
 * @Description:
 */
public class UdpHandler extends UdpProtocolHandler {
    private static Logger logger = LogManager.getLogger(org.cmcc.pcap.jpcap.udp.UdpHandler.class);
//    private UdpXdrGenerate uxg;
//
//    public UdpHandler() {
//        uxg =  new UdpXdrGenerate();
//    }

    @Override
    public void handle(UdpConnection udpConnection, String taskId) {

        logger.debug("start execute udp packet handler");

        logger.debug(new Date(udpConnection.lastPacketTime));//最后一个packet的处理时间(注:非抓包时间)
        long pcapTimestamp  = udpConnection.lastPacketTime;//pcap的timestamp的单位是秒，这里需要转成毫秒
        logger.debug(new Date(pcapTimestamp));//最后一个packet的抓包时间


        /**
         * Connection 内TCP 分段(segment)处理：乱序重组、重传清洗、TODO 默认保留前2个分段且可配？
         */
/*
		logger.debug("start tcp connection deal");
		tcpConnection.tcpConnectionPacketsDeal();//duplicate ack merge、retransmission and spurious retransmission merge、segments reassemble
		logger.debug("stop tcp connection deal");

		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (initial)  : "+tcpConnection.packetsNumInitial);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after duplicate ack merge)  : "+tcpConnection.packetsNumAfterDuplicateAckMerge);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after retransmission merge)  : "+tcpConnection.packetsNumAfterRetransmissionMerge);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after reorder packets)  : "+tcpConnection.packetsNumAfterReorderTcpPacket);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after reassemble) : "+tcpConnection.packetsNumAfterReassemble);
*/

//		for(TcpPacketDecorator tpd : tcpConnection.al){
//			logger.error("-----------------------------------");
//			logger.error(new String(tpd.getData()));
//			logger.error("-----------------------------------");
//		}
//		for(TcpPacketDecorator tpd : tcpConnection.retransmissionMergeAl){
//			logger.error("++++++++++++++++++++++++++++++++++++");
//			logger.error(new String(tpd.getData()));
//			logger.error("++++++++++++++++++++++++++++++++++++");
//		}
//		for(TcpPacketDecorator tpd : tcpConnection.reassembleAl){
//			logger.error("====================================");
//			logger.error(new String(tpd.getData()));
//			logger.error("====================================");
//		}


//        uxg.generate(udpConnection);
        logger.debug("this connection <"+udpConnection.connectionKey+"> save into udp packet entity !");

//        PcapUdpDao.save(uxg.udpEntityList, taskId);
//        uxg.udpEntityList.clear();
        logger.debug("this connection <"+udpConnection.connectionKey+"> saved into database !");



    }
}
